import React from 'react';

import {Button} from 'antd';

import {get, post} from '../../../logic/api';

import ObjectList from '../../components/ObjectList';
const Operation = ObjectList.Operation;
const InputEx = ObjectList.InputEx;
const SelectEx = ObjectList.SelectEx;
const FileEx = ObjectList.FileEx;

import router from '../../../utils/routerDecorator';
import QiniuUploadimage from '../../components/QiniuUploadimage';

export default class Edit extends React.Component {

  state = {
    parent:0
  }

  dataIndex = [
    {
      type:'select',
      subtitle:'一级分类',
      name:'parentId',
      options:[]
    },{
      subtitle:'中文名称',
      name:'cname',
      type:'textarea'
    },{
      subtitle:'英文名称',
      name:'ename',
      type:'textarea'
    },{
      subtitle:'描述',
      name:'describe',
      type:'textarea'
    },{
      subtitle:'图片',
      name:'headimage',
      type:'img'
    }

  ];

  componentWillMount(){

    for(var i=0;i<this.props.firstLevel.length;i++){
      this.dataIndex[0].options.push({
        value:""+(i+1),
        text:this.props.firstLevel[i]
      });
    }
  }

  render(){
    return <Operation
      width = {800}
      text = {this.props.text?this.props.text:"编辑"}
      dataIndex = {this.dataIndex}
      record = {this.props.record}
      fetchData = {async ()=>{
        if (this.props.fetchData){
          return await this.props.fetchData(this.props.record);
        }
        var ret = await get('/admin/cate/catesinfo/' + this.props.record.id);
        ret.data.parentId = ret.data.parentId + "";
        return ret;
      }}
      onOk = {async (r, f) => {
        var ret = await post('/admin/cate/upsert', {
          ...f,
          id:r.id
        });
        if (this.props.reload){
          this.props.reload();
        }
        
        return ret;
      }}
      onCancel = {async ()=>{
      }}
    />
  }
}